﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GavinCMS.Model.Entity;
using System.Data.Common;
using System.Data;

namespace GavinCMS.DAL
{
    public class PersonDAL : DALBase
    {
        public IList<Person> GetPersons(int pageIndex, int pageSize,ref int count)
        {
            DbCommand cmd = DefaultDB.GetSqlStringCommand(@"
select * from Person.Person
order by BusinessEntityID asc
OFFSET (@PageSize * (@PageIndex-1)) ROW FETCH NEXT @PageSize rows only
");

            DefaultDB.AddInParameter(cmd, "@PageIndex", DbType.Int32, pageIndex);
            DefaultDB.AddInParameter(cmd, "@PageSize", DbType.Int32, pageSize);

            var ds = DefaultDB.ExecuteDataSet(cmd);

            DbCommand cmd2 = DefaultDB.GetSqlStringCommand(@"
select count(*) from Person.Person
");
            var res = DefaultDB.ExecuteScalar(cmd2);
            count = Convert.ToInt32(res);

            return Gavin.Common.dbhelper.DataSetIList.DataSetToIList<Person>(ds, 0);
        }
    }
}
